import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { BossMoneyStrategyV2, IBossMoneyStrategyV2Option } from "./BossMoneyStrategyV2";
import { BossRabbitMoneyStrategy, IBossRabbitMoneyStrategyOption } from "./BossRabitMoneyStrategy";

export interface IBossReverseRabbitMoneyStrateyOption extends IBossRabbitMoneyStrategyOption {
    toReverseCount: number;
}

export class BossReverseMoneyStrategy extends BossRabbitMoneyStrategy implements IMoneyStrategy {

    toReverseCount: number = 0;
    reverse: boolean = false;
    option: IBossReverseRabbitMoneyStrateyOption;
    constructor(name: string, option?: IBossReverseRabbitMoneyStrateyOption | null) {
        super(name, option);
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                baseMoney: 10,
                winStep: 1,
                failStep: -1,
                prevMoney: 10,
                currentMoney: 10,
                prevPrevMoney: 0,
                toReverseCount: 2
            };
        }
        this.resetStrategy();
    }

    resetStrategy(): void {
        super.resetStrategy();
        this.reverse = false;
        this.toReverseCount = this.option.toReverseCount;
    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let [recmV, recmMoney] = super.toMoney(roundInfo, recmInfo, userRecm, baseMoney);
        if (!this.reverse && recmInfo.summaryInfo.lfail >= this.toReverseCount) {
            this.reverse = true;
        }
        else if (this.reverse && recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {
            this.reverse = false;
        }

        if (this.reverse && recmV != EnumBjlResult.U) {
            recmV = recmV == EnumBjlResult.Z ? EnumBjlResult.X : EnumBjlResult.Z;
        }

        return [recmV, recmMoney];
    }
    copyValue(valueObj: any): void {
        super.copyValue(valueObj);
        let obj = valueObj as BossReverseMoneyStrategy;
        if (obj) {
            this.reverse = obj.reverse;
        }
    }
}